package views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.Clientes;
import services.ClientesServices;

public class VistaCliente extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnTraer;
	private JPanel panePanel;
	private DefaultTableModel dtm;
	private JTable table;

	public VistaCliente() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Traer Clientes");
		setSize(new Dimension(911, 605));
		setOpacity(01);

		btnTraer = new JButton("Traer Clientes");
		btnTraer.setBounds(300, 50, 150, 50);

		panePanel = new JPanel();
		
		dtm = new DefaultTableModel(new Object[][] {}, new String[] {
				"ID", "Nombre", "UltimoIngreso" });
		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane tableSP = new JScrollPane(table);
		table.setModel(dtm);
		table.setBounds(300,100, 150, 50);
		
		OnTraerClick();

		getContentPane().add(panePanel);

		panePanel.add(btnTraer);
		panePanel.add(tableSP);
	}
	
	private void OnTraerClick(){
		btnTraer.addActionListener(new ActionListener(){		
			@Override
			public void actionPerformed(ActionEvent e) {
				ClientesServices cliser = new ClientesServices();
				for (Clientes q : cliser.GetListaClientes()) {

					DefaultTableModel defaultModel = (DefaultTableModel) table
							.getModel();
					Vector row = new Vector();
					row.add(q.getId());
					row.add(q.getNombre());
					row.add(q.getUltimoIngreso().toString());
					defaultModel.addRow(row);
					row = null;
					// table = defaultModel

					System.out.println("Cliente Cargado: " + q.getNombre());
				}
			}
		});
	}
}
